{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "import glob\n",
    "import itertools\n",
    "from ipywidgets import widgets, Layout\n",
    "import numpy as np\n",
    "import os\n",
    "import pandas as pd\n",
    "import plotly.io as pio\n",
    "import plotly.graph_objects as go\n",
    "\n",
    "from plotter.load_logfiles import load_logfiles\n",
    "\n",
    "pio.templates.default = \"plotly_white\"\n",
    "from IPython.core.interactiveshell import InteractiveShell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8d79e3e2c6c64e95b2b8ed7af8f6ff9b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "SelectMultiple(description='Experiments', layout=Layout(width='100%'), options=('log_sub_PointCloud512k_16-04-…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Define the folders where to look for experiment outputs\n",
    "os.chdir('../../../../experiment')\n",
    "logfiles = glob.glob('{}*'.format('log'))\n",
    "selected_logfiles =  widgets.SelectMultiple(\n",
    "    options=logfiles,\n",
    "    description='Experiments',\n",
    "    disabled=False,\n",
    "    layout=Layout(width='100%')\n",
    ")\n",
    "display(selected_logfiles)\n",
    "\n",
    "# Select the experiments to plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Experiment id': 'c9bf78b4-d94a-4772-88c4-edd324d7238f',\n",
       " 'Performance Test Version': '7284ec0',\n",
       " 'Logfile name': 'log_sub_PointCloud512k_16-04-2021_10-23-28',\n",
       " 'Communication mean': 'ROS2PollingSubscription',\n",
       " 'RMW Implementation': 'rmw_cyclone_dds',\n",
       " 'DDS domain id': '0',\n",
       " 'QOS': 'Reliability: BEST_EFFORT Durability: VOLATILE History kind: KEEP_LAST History depth: 100 Sync. pub/sub: 0',\n",
       " 'Publishing rate': '100',\n",
       " 'Topic name': 'PointCloud512k',\n",
       " 'Maximum runtime (sec)': '35',\n",
       " 'Number of publishers': '0',\n",
       " 'Number of subscribers': '1',\n",
       " 'Memory check enabled': '0',\n",
       " 'Use ros SHM': '0',\n",
       " 'Use single participant': '0',\n",
       " 'Not using waitset': '0',\n",
       " 'Not using Connext DDS Micro INTRA': '0',\n",
       " 'With security': '0',\n",
       " 'Roundtrip Mode': 'NONE',\n",
       " 'Ignore seconds from beginning': '5'}"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "{'Experiment id': 'a87a23d6-7c19-47d0-b577-66d094f26100',\n",
       " 'Performance Test Version': '7284ec0',\n",
       " 'Logfile name': 'log_pub_PointCloud512k_16-04-2021_10-20-17',\n",
       " 'Communication mean': 'ROS2PollingSubscription',\n",
       " 'RMW Implementation': 'rmw_cyclone_dds',\n",
       " 'DDS domain id': '0',\n",
       " 'QOS': 'Reliability: BEST_EFFORT Durability: VOLATILE History kind: KEEP_LAST History depth: 100 Sync. pub/sub: 0',\n",
       " 'Publishing rate': '100',\n",
       " 'Topic name': 'PointCloud512k',\n",
       " 'Maximum runtime (sec)': '35',\n",
       " 'Number of publishers': '1',\n",
       " 'Number of subscribers': '0',\n",
       " 'Memory check enabled': '0',\n",
       " 'Use ros SHM': '0',\n",
       " 'Use single participant': '0',\n",
       " 'Not using waitset': '0',\n",
       " 'Not using Connext DDS Micro INTRA': '0',\n",
       " 'With security': '0',\n",
       " 'Roundtrip Mode': 'NONE',\n",
       " 'Ignore seconds from beginning': '5'}"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Display selected experiment properties\n",
    "InteractiveShell.ast_node_interactivity = \"all\"\n",
    "\n",
    "headers, dataframes = load_logfiles(selected_logfiles)\n",
    "\n",
    "for idx, header in enumerate(headers):\n",
    "    display(header)\n",
    "    \n",
    "InteractiveShell.ast_node_interactivity = \"last\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9144e6aff3454d66984b95c000081618",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "FigureWidget({\n",
       "    'data': [{'marker': {'color': '#4363d8', 'symbol': 'x'},\n",
       "              'mode': 'markers',\n",
       " …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "colors = ['#4363d8','#800000','#f58231','#e6beff']\n",
    "\n",
    "# Plot latencies\n",
    "figure_latencies = go.FigureWidget()\n",
    "figure_latencies.layout.xaxis.title = 'Time (s)'\n",
    "figure_latencies.layout.yaxis.title = 'Latencies (ms)'\n",
    "\n",
    "for idx, experiment in enumerate(dataframes):\n",
    "\n",
    "    figure_latencies.add_scatter(x=experiment['T_experiment'],\n",
    "                                 y=experiment['latency_max (ms)'],\n",
    "                                 mode='markers', marker_color=colors[idx],\n",
    "                                 marker_symbol='x',\n",
    "                                 name= 'latency_max',\n",
    "                                 text=headers[idx]['Logfile name']);\n",
    "    figure_latencies.add_scatter(x=experiment['T_experiment'],\n",
    "                                 y=experiment['latency_mean (ms)'],\n",
    "                                 mode='markers', marker_color=colors[idx],\n",
    "                                 marker_symbol='triangle-up',\n",
    "                                 name='latency_mean',\n",
    "                                 text=headers[idx]['Logfile name']);\n",
    "    figure_latencies.add_scatter(x=experiment['T_experiment'],\n",
    "                                 y=experiment['latency_min (ms)'],\n",
    "                                 mode='markers', marker_color=colors[idx],\n",
    "                                 name='latency_min',\n",
    "                                 text=headers[idx]['Logfile name'])\n",
    "\n",
    "figure_latencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot CPU usage\n",
    "figure_cpu_usage = go.FigureWidget()\n",
    "figure_cpu_usage.layout.xaxis.title = 'Time (s)'\n",
    "figure_cpu_usage.layout.yaxis.title = 'CPU usage (%)'\n",
    "\n",
    "for idx, experiment in enumerate(dataframes):\n",
    "\n",
    "    figure_cpu_usage.add_scatter(x=experiment['T_experiment'],\n",
    "                                 y=experiment['cpu_usage (%)'],\n",
    "                                 mode='markers', marker_color=colors[idx],\n",
    "                                 marker_symbol='x',\n",
    "                                 name= 'cpu_usage',\n",
    "                                 text=headers[idx]['Logfile name']);\n",
    "\n",
    "figure_cpu_usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot memory consumption\n",
    "figure_memory_usage = go.FigureWidget()\n",
    "figure_memory_usage.layout.xaxis.title = 'Time (s)'\n",
    "figure_memory_usage.layout.yaxis.title = 'Memory consumption (MB)'\n",
    "\n",
    "for idx, experiment in enumerate(dataframes):\n",
    "\n",
    "    figure_memory_usage.add_scatter(x=experiment['T_experiment'],\n",
    "                                    y=experiment['ru_maxrss']/1024,\n",
    "                                    mode='markers', marker_color=colors[idx],\n",
    "                                    marker_symbol='x',\n",
    "                                    name= 'ru_maxrss',\n",
    "                                    text=headers[idx]['Logfile name']);\n",
    "\n",
    "figure_memory_usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
